<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!--card卡片-->
    <el-card class="box-card">
      <el-button type="primary" @click="addCateShow">添加分类</el-button>
      <!--table表格-->
      <tree-table class="tree-table"
                  :selection-type="false"
                  :expand-type="false"
                  show-index
                  :show-row-hover="false"
                  index-text="#"
                  :data="categoriesList"
                  :columns="columns"
                  border>
        <template slot="isok" scope="scope">
          <i class="el-icon-error" v-if="scope.row.cat_deleted" style="color:red"></i>
          <i class="el-icon-success" v-else style="color:lightgreen"></i>
        </template>
        <template slot="level" scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <template slot="operation" scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope)">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
      <!--分页-->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="categoriesQuery.num"
        :page-size="categoriesQuery.pagesize"
        layout="prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!---->
    <!---->
    <!---->
    <!--添加分类-->
    <el-dialog
      ref="addCateRef"
      @close="addCateClose"
      title="添加分类"
      :visible.sync="addCateDialog"
      width="50%">
      <el-form :model="addCateForm" :rules="addCateRules" ref="addCateRef" label-width="100px">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：" style="position: relative" class="block">
          <el-cascader
            clearable
            v-model="value"
            :options="options"
            :props="props"
            @change="changeHandle">
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialog = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        // 分类列表
        categoriesList: [],
        // 分类参数
        categoriesQuery: {
          type: '',
          pagenum: 1,
          pagesize: 5
        },
        total: 0,
        defaultProps: {
          children: 'children',
          label: 'cat_name'
        },
        columns: [
          {
            label: '分类名称',
            prop: 'cat_name'
          },
          {
            label: '是否有效',
            type: 'template',
            template: 'isok'
          },
          {
            label: '排序',
            prop: 'cat_level',
            type: 'template',
            template: 'level'
          },
          {
            label: '操作',
            type: 'template',
            template: 'operation'
          }
        ],
        // 添加分类
        addCateDialog: false,
        addCateForm: {
          cat_name: '',
          cat_pid: 0,
          cat_level: 0
        },
        addCateRules: {
          cat_name: [
            {
              required: true,
              message: '请输入分类名称',
              trigger: 'blur'
            }
          ]
        },
        value: [],
        props: {
          expandTrigger: 'hover',
          label: 'cat_name',
          value: 'cat_id',
          children: 'children',
          checkStrictly: true
        },
        options: [],
        selectValue: []
      }
    },
    created () {
      this.getCategories()
    },
    methods: {
      // 获取前两集数据列表
      getCateTwo: async function () {
        const { data: res } = await this.$http.get('categories', {
          params: {
            type: 2
          }
        })
        console.log(res)
        if (res.meta.status !== 200) return this.$message.error('获取前两级商品分类列表失败！')
        this.options = res.data
      },
      // 获取分类列表
      getCategories: async function () {
        const { data: res } = await this.$http.get('categories', {
          params: this.categoriesQuery
        })
        this.categoriesList = res.data.result
        this.total = res.data.total
      },
      // 分页当前页
      handleCurrentChange (val) {
        this.categoriesQuery.pagenum = val
        this.getCategories()
      },
      // 展示添加分类dialog
      addCateShow: function () {
        this.getCateTwo()
        this.addCateDialog = true
      },
      // 级联选择器选中的值
      changeHandle: function(value) {
        this.selectValue = value
      },
      // 提交添加分类
      addCate: async function () {
        if (this.selectValue.length > 0) {
          this.addCateForm.cat_pid = this.selectValue[this.selectValue.length - 1]
          this.addCateForm.cat_level = this.selectValue.length
        } else {
          this.addCateForm.cat_pid = 0
          this.addCateForm.cat_level = 0
        }
        this.$refs.addCateRef.validate(async valid => {
          if (!valid) return
          const { data: res } = await this.$http.post('categories', this.addCateForm)
          if (res.meta.status !== 201) return this.$message.error('商品分类添加失败！')
          this.$message.success('商品分类添加成功！')
          this.addCateDialog = false
          this.getCategories()
        })
      },
      // 关闭添加分类，清空数据
      addCateClose: function () {
        this.$refs.addCateRef.resetFields()
        this.value = []
        this.addCateForm = {
          cat_name: '',
          cat_pid: 0,
          cat_level: 0
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .right {
    background-color: #67af6b;
    border-radius: 50%;
    color: #fff
  }
  .tree-table {
    margin-top: 15px;
  }
  .el-cascader {
    width: 100%;
  }
</style>
